/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package me.yokeyword.sample.demo_flow.ui.fragment.home;

import me.yokeyword.sample.ResourceTable;
import me.yokeyword.sample.demo_flow.base.MySupportFraction;

import ohos.aafwk.ability.fraction.FractionAbility;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.TextField;
import ohos.utils.PacMap;

public class ModifyDetailFraction extends MySupportFraction {
    private static final String ARG_TITLE = "arg_title";
    private TextField mEtModiyTitle;
    private Button mBtnModify, mBtnNext;
    private String mTitle;
    private FractionAbility mfractionability;

    public static ModifyDetailFraction newInstance(String title) {
        PacMap args = new PacMap();
        ModifyDetailFraction fraction = new ModifyDetailFraction();
        args.putString(ARG_TITLE, title);
        fraction.setArguments(args);
        return fraction;
    }

    @Override
    protected void onActive() {
        super.onActive();
        mfractionability = getFractionAbility();
    }

    @Override
    protected Component onComponentAttached(LayoutScatter scatter, ComponentContainer container, Intent intent) {
        Component component = scatter.parse(ResourceTable.Layout_layout_fraction_modify_detail, container, false);
        initComponent(component);
        return component;
    }

    private void initComponent(Component component) {
        PacMap bundle = getArguments();
        if (bundle != null) {
            mTitle = bundle.getString(ARG_TITLE);
        }
        mEtModiyTitle = (TextField) component.findComponentById(ResourceTable.Id_et_modify_title);
        mEtModiyTitle.setText(mTitle);
        mBtnModify = (Button) component.findComponentById(ResourceTable.Id_btn_modify);
        mBtnNext = (Button) component.findComponentById(ResourceTable.Id_btn_next);
        mBtnModify.setClickedListener(component1 -> {
            PacMap bundle1 = new PacMap();
            bundle1.putString(DetailFraction.KEY_RESULT_TITLE, mEtModiyTitle.getText());
            setFractionResult(RESULT_OK, bundle1);
            mfractionability.getUITaskDispatcher().asyncDispatch(() -> {
                ohos.agp.window.dialog.ToastDialog toastDialog = new ohos.agp.window.dialog.ToastDialog(getFractionAbility());
                toastDialog.setAutoClosable(false);
                toastDialog.setContentText("Modify successfully!");
                toastDialog.show();
            });
        });
        mBtnNext.setClickedListener(component12 -> start(CycleFraction.newInstance(1)));
    }
}
